package com.vue.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vue.pojo.User;
import com.vue.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/show")
    public ResponseEntity<Page<User>> findAll(Integer pagenumber, Integer pagesize) {
        Page<User> all = userService.findAll(pagenumber, pagesize);
        return ResponseEntity.ok(all);
    }

    @RequestMapping("/up")
    public ResponseEntity<String> upOne(String userName, String realName, String address, String sex, String phone, Integer id) {
        User user = new User();
        user.setAddress(address);
        user.setPhone(phone);
        user.setRealName(realName);
        user.setUserName(userName);
        user.setSex(sex);
        user.setId(id);
        user.setRole(2);
        user.setShoww("true");
        int i = userService.upOne(user);
        if (i == 1) {
            return new ResponseEntity<>("修改成功", HttpStatus.OK);
        } else {
            return new ResponseEntity<>("修改失败", HttpStatus.NOT_FOUND);
        }
    }

    @RequestMapping("/del")
    public ResponseEntity<String> delOne(String id) {
        int i = userService.delOne(Integer.parseInt(id));
        if (i == 1) {
            return new ResponseEntity<>("删除成功", HttpStatus.OK);
        } else {
            return new ResponseEntity<>("删除失败", HttpStatus.NOT_FOUND);
        }
    }

    @RequestMapping("/add")
    public ResponseEntity<String> addOne(String userName, String realName, String address, String sex, String phone, String pwd) {
        User user1 = userService.finOne(userName,1);
        if (user1 == null) {
            User user = new User();
            user.setAddress(address);
            user.setPhone(phone);
            user.setRealName(realName);
            user.setUserName(userName);
            user.setSex(sex);
            user.setRole(2);
            user.setShoww("true");
            user.setPwd(pwd);
            user.setCreateDate(new Date());
            String hashpw = BCrypt.hashpw(user.getPwd(), BCrypt.gensalt());
            user.setPwd(hashpw);
            userService.insOne(user);
            return new ResponseEntity<>("添加成功", HttpStatus.OK);
        } else {
            return new ResponseEntity<>("添加失败", HttpStatus.NOT_FOUND);
        }


    }

    @RequestMapping("/uppas")
    public ResponseEntity<String> uppas(String oldpas, String newpas, String newpas1, String id) {
        if (newpas != newpas1) {
            return new ResponseEntity<>("修改失败", HttpStatus.NOT_FOUND);
        } else {
            User user = userService.findOnee(Integer.parseInt(id));
            boolean checkpw = BCrypt.checkpw(oldpas, user.getPwd());
            if (checkpw) {
                User user1 = new User();
                user1.setPwd(BCrypt.hashpw(newpas, BCrypt.gensalt()));
                user1.setId(Integer.parseInt(id));
                int i = userService.upOne(user1);
                if (i == 1) {
                    return new ResponseEntity<>("修改成功", HttpStatus.OK);
                } else {
                    return new ResponseEntity<>("修改失败", HttpStatus.NOT_FOUND);
                }
            } else {
                return new ResponseEntity<>("修改失败", HttpStatus.NOT_FOUND);
            }
        }
    }

    @RequestMapping("/login")
    public ResponseEntity<String> login(String username, String password) {
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            return new ResponseEntity<>("不存在", HttpStatus.OK);
        }else{
            User user = userService.finOne(username,2);
            if(user==null){
                return new ResponseEntity<>("不存在", HttpStatus.OK);
            }else{
                if(BCrypt.checkpw(password,user.getPwd())){
                    return new ResponseEntity<>("正确", HttpStatus.OK);
                }else{
                    return new ResponseEntity<>("不存在", HttpStatus.OK);
                }
            }
        }

    }
}
